/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cw.bo;

import java.util.Vector;
import org.cw.bean.Establishment;
import org.cw.bean.Reviews;
import org.cw.dao.ReviewDAO;

/**
 *
 * @author SVN - Team
 */
public class ReviewBO {
    private ReviewDAO reviewDAO = new ReviewDAO();
    private static ReviewBO instance;
    public static ReviewBO getInstance() {
        if(instance == null) {
            instance = new ReviewBO();
        }
        return instance;
    }

    public boolean add(Reviews r) {
        return reviewDAO.write(r);
    }

    public boolean edit(Reviews oObj, Reviews nObj) {
        return reviewDAO.update(oObj, nObj);
    }

    public boolean delete(Reviews rRe) {
        return reviewDAO.delete(rRe);
    }

    public Vector list() {
        return null;
    }

    public Reviews findById(int idx) {
        return null;
    }

    public Reviews getReviewByData(Establishment est, String strData) {
        Reviews r = new Reviews();
        Vector vRe = est.getListReview();
        for(int i = 0; i < vRe.size(); i++) {
            r = (Reviews) vRe.elementAt(i);
            if(strData.getBytes().equals(convertStringToByte(r))) {
                return r;
            }
        }
        return r;
    }

    public byte[] convertStringToByte(Reviews r) {
        String strData = "- Date:" + r.getDate() + "\n"
                        + "- Type:" + r.getTypeOfMeal() + "\n"
                        + "- Cost:" + r.getCost() + "$\n"
                        + "- OverRall rate:" + getStarByPoint(r.getOverallRating()) + "(" + r.getOverallRating() + "/5)" + "\n"
                        + "- Rating for Services:" + getStarByPoint(r.getRateOfServices()) + "(" + r.getRateOfServices() + "/5)" + "\n"
                        + "- Rating for atmosphere:" + getStarByPoint(r.getRateOfAtmosphere()) + "(" + r.getRateOfAtmosphere() + "/5)" + "\n"
                        + "- Rating for food:" + getStarByPoint(r.getRateOfFood()) + "(" + r.getRateOfFood() + "/5)" + "\n"
                        + "- Comment:" + r.getComment() + "\n";
        return strData.getBytes();
    }
    private String getStarByPoint(int pRate) {
        String strStar = "";
        for (int i = 0; i < pRate; i++) {
            strStar += "★";
        }
        return strStar;
    }

    public Vector getBestReview(Establishment est) {
        Vector listBestReview = new Vector();
        Reviews best = (Reviews) est.getListReview().elementAt(0);
        for(int i = 0; i < est.getListReview().size(); i++) {
            Reviews temp = (Reviews) est.getListReview().elementAt(i);
            if(best.getOverallRating() < temp.getOverallRating()) {
                best = temp;
            }
        }
        for(int i =0; i < est.getListReview().size(); i++) {
            Reviews tmpBest = (Reviews) est.getListReview().elementAt(i);
            if(best.getOverallRating() == tmpBest.getOverallRating()) {
                listBestReview.addElement(tmpBest);
            }
        }
        return listBestReview;
    }
}
